package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.sc.nft.dao.EcologyHatchExtraRewardsMethodConfDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.entity.EcologyHatchConf;
import com.sc.nft.entity.EcologyHatchExtraRewardsMethodConf;
import com.sc.nft.entity.SecondCollection;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsMethodConfDTO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchExtraRewardsConfigService;
import com.sc.nft.service.EcologyHatchExtraRewardsMethodConfService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author Radish
 * @Description:
 * @date 2024-11-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchExtraRewardsMethodConfServiceImpl  implements EcologyHatchExtraRewardsMethodConfService {

    private final EcologyHatchConfDao ecologyHatchConfDao;

    private final EcologyHatchExtraRewardsMethodConfDao ecologyHatchExtraRewardsMethodConfDao;

    private final EcologyHatchExtraRewardsConfigService ecologyHatchExtraRewardsConfigService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchExtraRewardsConfigMethodConf(Long ecologyHatchId, Long extraRewardsId,List<EcologyHatchExtraRewardsMethodConfDTO> ecologyHatchExtraRewardsMethodConfDTOS){

//        List<EcologyHatchExtraRewardsMethodConf> methodConfDaoListByEcologyHatchId = ecologyHatchExtraRewardsMethodConfDao.findListByEcologyHatchId(ecologyHatchId);
//        BigDecimal oldProbabilitySum = methodConfDaoListByEcologyHatchId.stream().map(EcologyHatchExtraRewardsMethodConf::getProbability).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal probabilitySum = ecologyHatchExtraRewardsMethodConfDTOS.stream().map(EcologyHatchExtraRewardsMethodConfDTO::getProbability).reduce(BigDecimal.ZERO, BigDecimal::add);
        //判断oldProbabilitySum.add(probabilitySum)不能大于一百
        Assert.isTrue(probabilitySum.compareTo(BigDecimal.valueOf(100)) == 0, () -> new GlobalRunTimeException("总概率需为100"));

        for (EcologyHatchExtraRewardsMethodConfDTO ecologyHatchExtraRewardsMethodConfDTO : ecologyHatchExtraRewardsMethodConfDTOS) {

            // 获取奖励配置方式id
            Long methodId = ecologyHatchExtraRewardsMethodConfDTO.getId();
            // 奖励配置方式
            if (Objects.isNull(methodId)) {
                EcologyHatchExtraRewardsMethodConf ecologyHatchExtraRewardsMethodConf = new EcologyHatchExtraRewardsMethodConf();
                ecologyHatchExtraRewardsMethodConf.setEcologyHatchId(ecologyHatchId);
                ecologyHatchExtraRewardsMethodConf.setExtraRewardsId(extraRewardsId);
                ecologyHatchExtraRewardsMethodConf.setProbability(ecologyHatchExtraRewardsMethodConfDTO.getProbability());
                ecologyHatchExtraRewardsMethodConf.insert();
                methodId = ecologyHatchExtraRewardsMethodConf.getId();
            } else {
                Assert.isTrue(ecologyHatchExtraRewardsMethodConfDao.updateById(methodId, ecologyHatchExtraRewardsMethodConfDTO.getProbability()), () -> new GlobalRunTimeException("修改熔炼方式失败"));
            }

            //编辑额外奖励
            ecologyHatchExtraRewardsConfigService.editEcologyHatchExtraRewardsConfig(ecologyHatchId,extraRewardsId,methodId,
                    ecologyHatchExtraRewardsMethodConfDTO.getEcologyHatchExtraRewardsConfigSaveDTOS());
        }

    }
}
